#include "string.h"

void int2ascii(int num, char *str) {
    int i = 0, sign;
    if((sign = num) < 0) num = -num;
    do {
        str[i++] = (num % 10) + '0';
    } while ((num /= 10) != 0);
    if(sign < 0) str[i++] = '-';
    str[i] = '\0';
    reverse(str);
}

void reverse(char *str) {
    int i = 0, j = strlen(str) - 1;
    while (i < j)
    {
        char tmp = str[i];
        str[i] = str[j];
        str[j] = tmp;
        i++;
        j--;
    }
}

int strlen(char *str) {
    int i = 0;
    while (str[i] != '\0') i++;
    return i;
}

void append(char *str, char c) {
    int len = strlen(str);
    str[len] = c;
    str[len+1] = '\0';
}

void backspace(char *str) {
    int len = strlen(str);
    if(len > 0) {
        str[len- 1] = '\0';
    }
}

// 0 相等 >0 str1>str2 <0 str1<str2
int strcmp(char *str1, char *str2) {
    int i;
    for(i = 0; str1[i] == str2[i]; i++) {
        if(str1[i] == '\0') return 0;
    }
    return str1[i] - str2[i];
}
